package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import his.entity.ConstantItem;
import his.entity.Department;
import his.persistence.DepartmentMapper;
import his.service.DepartmentService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public CommonResponse<String> convert_id_to_name(int deptid) {
        QueryWrapper<Department> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ID",deptid);
        Department department=departmentMapper.selectOne(queryWrapper1);
        if(department==null){
            return CommonResponse.createForError("未查找到该科室");
        }else{
            return CommonResponse.createForSuccess(department.getDeptName());
        }

    }

    @Override
    public CommonResponse<List<Department>> update_departments_byid(int id, String deptCode, String deptName, int deptCategoryID, int deptType) {

        QueryWrapper<Department> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ID",id);
        List<Department> departmentList1=departmentMapper.selectList(queryWrapper1);
        if(departmentList1.size()>0){
            UpdateWrapper<Department> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("ID",id);
            Department department=new Department();
            department.setDeptCode(deptCode);
            department.setDeptName(deptName);
            department.setDelMark(1);
            department.setDeptCategoryID(deptCategoryID);
            department.setDeptType(deptType);
            int result=departmentMapper.update(department,updateWrapper);
            QueryWrapper<Department> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ID",id);
            List<Department> departmentList2=departmentMapper.selectList(queryWrapper2);
            return CommonResponse.createForSuccess(departmentList2);
        }else{
            return CommonResponse.createForError("修改失败");
        }


    }

    @Override
    public CommonResponse<List<Department>> delete_departments_byid(int id) {

        QueryWrapper<Department> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ID",id);
        List<Department> departmentList=departmentMapper.selectList(queryWrapper1);
        if(departmentList.size()>0){
            QueryWrapper<Department> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ID",id);
            int result=departmentMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess(departmentList);
        }else{
            return CommonResponse.createForError("删除失败");
        }

    }

    @Override
    public CommonResponse<List<Department>> getall_departments() {
        QueryWrapper<Department> queryWrapper=new QueryWrapper<>();
        List<Department> departmentList=departmentMapper.selectList(queryWrapper);
        if(departmentList.size()>0){
            return CommonResponse.createForSuccess(departmentList);
        }else {
            return CommonResponse.createForError("查找失败");
        }

    }

    @Override
    public CommonResponse<List<Department>> get_departments_bycodename(String keywords) {

        QueryWrapper<Department> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DeptCode",keywords);
        List<Department> departmentList1=departmentMapper.selectList(queryWrapper1);
        if(departmentList1.size()>0){
            return CommonResponse.createForSuccess(departmentList1);
        }else{
            QueryWrapper<Department> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DeptName",keywords);
            List<Department> departmentList2=departmentMapper.selectList(queryWrapper2);
            if(departmentList2.size()>0){
                return CommonResponse.createForSuccess(departmentList2);
            }else{
                return CommonResponse.createForSuccessMessage("未找到对应的科室");
            }
        }
    }

    @Override
    public CommonResponse new_department(String deptCode, String deptName, int deptCategoryID, int deptType) {

        QueryWrapper<Department> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DeptName",deptName);
        List<Department> departmentList=departmentMapper.selectList(queryWrapper1);
        if(departmentList.size()>0){
            return CommonResponse.createForError("新增失败，已经存在");
        }else{
            Department department=new Department();
            department.setDelMark(1);
            department.setDeptCode(deptCode);
            department.setDeptName(deptName);
            department.setDeptType(deptType);
            department.setDeptCategoryID(deptCategoryID);
            departmentMapper.insert(department);
            QueryWrapper<Department> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DeptName",deptName);
            List<Department> departmentList1=departmentMapper.selectList(queryWrapper2);
            return CommonResponse.createForSuccess(departmentList1);
        }





    }
}
